package com.szqhyxj.youxiangju.biz.creditCardEvaluation.evaluationRecord;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.equipmentActivation.activationrecord.ActivationRecordModel;
import com.szqhyxj.youxiangju.biz.web.WebActivity;

import java.util.List;

/**
 * Created by leung on 2018/11/4.
 */

public class EvaluationRecordAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<RowsItem> mDatas;
    private IStartActivityListener listener;
    //MyAdapter需要一个Context，通过Context获得Layout.inflater，然后通过inflater加载item的布局
    public EvaluationRecordAdapter(Context context, List<RowsItem> datas) {

        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    public void setListener(IStartActivityListener listener){
        this.listener = listener;
    }

    //返回数据集的长度
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //这个方法才是重点，我们要为它编写一个ViewHolder
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.activation_record_evaluation_item, parent, false); //加载布局
            holder = new ViewHolder();
            holder.itemEvaluation = convertView.findViewById(R.id.item_evaluation);
            holder.bankName = convertView.findViewById(R.id.bankName);
            holder.bankCardNo = convertView.findViewById(R.id.bankCardNo);
            holder.time = convertView.findViewById(R.id.time_evaluation);
            holder.status = convertView.findViewById(R.id.status);

            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
        }
        holder.bankName.setText(mDatas.get(position).getBtName());
        String bankcardno = mDatas.get(position).getBankCardNo();
        if (bankcardno==null||bankcardno.equals("")){

        }else{
            String bankcardnosub = bankcardno.substring(bankcardno.length()-4,bankcardno.length());
            String cardno = "("+bankcardnosub+")";
            holder.bankCardNo.setText(cardno);
        }


        holder.status.setText(mDatas.get(position).getEvaluationStatus());
        if (mDatas.get(position).getAddDate() == null) {
            holder.time.setText("");
        } else {
            String addDate = mDatas.get(position).getAddDate();
            holder.time.setText(addDate + "");
        }

        holder.itemEvaluation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDatas.get(position).getEvaluationStatus().equals("测评失败")||mDatas.get(position).getEvaluationStatus().equals("测评中")){

                }else{
                    listener.onStartActivityListener(mDatas.get(position).getReportUrl());
                }

            }
        });

        return convertView;
    }

    interface IStartActivityListener {
        void onStartActivityListener(String url);
    }

    ;


    //这个ViewHolder只能服务于当前这个特定的adapter，因为ViewHolder里会指定item的控件，不同的ListView，item可能不同，所以ViewHolder写成一个私有的类
    private class ViewHolder {
        LinearLayout itemEvaluation;
        TextView bankName;
        TextView bankCardNo;
        TextView status;
        TextView time;

    }


}